import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import '../../../common/routers/names.dart';
import '../../../common/widgets/Avatar.dart';
import 'index.dart';

class SearchchatPage extends GetView<SearchchatController> {
  const SearchchatPage({super.key});

  // 主视图
  Widget _buildView(SearchchatController controller) {
    return Obx(() {
      return Container(
        color: Colors.white,
        child: Column(
          children: [
            // 搜索栏
            SearchBar(
              hintText: "请输入聊天内容",
              controller: controller.searchMessageContentController,
              leading: const Icon(Icons.search, color: Colors.grey),
              textInputAction: TextInputAction.search,
              onSubmitted: (value) => controller.fetchData(),
              shape: WidgetStateProperty.all(const RoundedRectangleBorder(borderRadius: BorderRadius.zero)),
              trailing: [TextButton(onPressed: controller.fetchData, child: const Text("搜索", style: TextStyle(color: Colors.blue)))],
              backgroundColor: WidgetStateProperty.all(Colors.white),
              overlayColor: WidgetStateProperty.all(Colors.transparent),
              elevation: WidgetStateProperty.all(3),
            ),

            // 搜索结果区域
            Expanded(
              child:
                  controller.searchUserList.isEmpty
                      ? Center(child: Text(controller.hasSearch.value, style: TextStyle(color: Colors.grey.shade500, fontSize: 15)))
                      : ListView.separated(
                        physics: const BouncingScrollPhysics(),
                        itemCount: controller.searchUserList.length,
                        separatorBuilder: (context, index) => Divider(color: Colors.grey.shade300, height: 0.5, thickness: 0.5, indent: 70),
                        itemBuilder: (context, index) {
                          final chat = controller.searchUserList[index];
                          return ListTile(
                            leading: Avatar(avatar: chat.avatar),
                            title: Text(chat.name),
                            subtitle: Text(chat.lastContent),
                            onTap:
                                () => Get.toNamed(
                                  RouteNames.chatdetail,
                                  arguments: {
                                    "userId": controller.user.value?.uid,
                                    "friendId": chat.userId,
                                    "friendName": chat.name,
                                    "friendAvatar": chat.avatar,
                                    "targetMessageId": chat.messageId,
                                  },
                                ),
                          );
                        },
                      ),
            ),
          ],
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchchatController>(
      init: SearchchatController(),
      id: "searchchat",
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(title: const Text("搜索聊天"), centerTitle: true, backgroundColor: Colors.white),
          body: SafeArea(child: _buildView(controller)),
        );
      },
    );
  }
}
